﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jlob.OpenErpNet.Entities;
using System.Collections.Generic;
using System.Linq;

namespace Jlob.OpenErpNet.Test
{
    [TestClass]
    public class AttendanceTest
    {

        private const string server = "http://192.168.11.129:8069";

        [TestMethod]
        public void processAttendance()
        {
            string id = "55555555";
            string sourceId = "111111111";
            DateTime when = DateTime.Now;
            Employee employee;
            OpenErpService openErpService = new OpenErpService(server, "XXX", "admin", "admin");
            employee = openErpService.GetEntities<Employee>(e => e.IdentificationNumber.StartsWith(id)).FirstOrDefault();
            List<string> attendanceActionValues = new List<string>();
            attendanceActionValues.Add(Attendance.Sign_in);
            attendanceActionValues.Add(Attendance.Sign_out);
            Attendance lastAttendance = openErpService.GetEntities<Attendance>(a => a.EmployeeId == employee.id 
                                                                                        && a.Date < when
                                                                                        && attendanceActionValues.Contains(a.Action)).FirstOrDefault();
            Sheet currentSheet = openErpService.GetEntities<Sheet>(a => a.DateFrom <= when && when >= a.DateTo && a.EmployeeId == employee.id).FirstOrDefault();
            if (currentSheet == null)
            {
                //Crear la nueva
                currentSheet = new Sheet();
                DateTime date = when;
                while (date.DayOfWeek != DayOfWeek.Monday)
                {
                    date = date.AddDays(-1);
                }
                currentSheet.DateFrom = date;
                date = when;
                while (date.DayOfWeek != DayOfWeek.Sunday)
                {
                    date = date.AddDays(1);
                }
                currentSheet.DateTo = date;
                currentSheet.EmployeeId = employee.id;
                currentSheet.UserId = employee.UserId.GetValueOrDefault();
                openErpService.AddEntity<Sheet>(currentSheet);
            }
            AnalyticAccount contract = openErpService.GetEntities<AnalyticAccount>(a => a.TermsAndConditions.StartsWith(sourceId)).FirstOrDefault();

            Attendance registedAttendance = new Attendance();
            if (lastAttendance == null)
            {
                registedAttendance.Action = Attendance.Sign_in;
            }
            else
            {
                if (lastAttendance.Action.Equals(Attendance.Sign_in))
                {
                    registedAttendance.Action = Attendance.Sign_out;
                }
                else
                {
                    registedAttendance.Action = Attendance.Sign_in;
                }
            }
            registedAttendance.EmployeeId = employee.id;
            registedAttendance.Date = when.ToUniversalTime();
            if(registedAttendance.Action.Equals(Attendance.Sign_out))
            {
                //If sign_out, add a timesheet with hours from sign_in
                    TimeSpan diference = when.ToUniversalTime() - lastAttendance.Date;
                    Timesheet timesheet = new Timesheet();
                    timesheet.Contract = contract.Id;
                    timesheet.Description = "";
                    timesheet.TotalTime = (double)diference.Hours + Math.Round((double)diference.Minutes / 60, 2);
                    timesheet.UserId = employee.UserId.GetValueOrDefault();
                    timesheet.Date = when.ToUniversalTime();
                    //timesheet.JournalId = employee.JournalId.GetValueOrDefault();
                    timesheet.SheetId = currentSheet.Id;
                    openErpService.AddEntity<Timesheet>(timesheet);
            }
            openErpService.AddEntity<Attendance>(registedAttendance);
        }

        [TestMethod]
        public void Test_Collection_contains_method()
        {
            Employee em = new Employee();
            OpenErpService openErpService = new OpenErpService("http://192.168.11.129:8069", "POLTEST", "admin", "admin");
            List<string> attendanceActionValues = new List<string>();
            attendanceActionValues.Add(Attendance.Sign_in);
            attendanceActionValues.Add(Attendance.Sign_out);
            Attendance lastAttendance = openErpService.GetEntities<Attendance>(a => attendanceActionValues.Contains(a.Action)).FirstOrDefault();
        }

       
    }
}
